package com.sf.web.controller;

import com.sf.domain.Department;
import com.sf.domain.Employee;
import com.sf.qo.EmployeeQueryObject;
import com.sf.qo.QueryObject;
import com.sf.service.IDepartment;
import com.sf.service.IEmployee;
import com.sf.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private IEmployee employee;
    @Autowired
    private IDepartment department;
    @RequestMapping("/login")
    public String login(Employee employee1, HttpSession httpSession, Model model,EmployeeQueryObject qo) {

        if (employee.Login(employee1)!=null) {

            httpSession.setAttribute("username",employee1.getUsername());
            PageResult<Department> pageResult = department.query(qo);
            model.addAttribute("pageResult", pageResult);
            return "department/list";

        }else {
            return "redirect:/login.jsp";
        }
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("department/list");
//        Employee employee2 = employee.Login(employee1);
//        if (employee2 != null) {
//            return modelAndView;
//        }else {
//
//            return modelAndView;
//        }
    }
    @RequestMapping("/employee/list")
    public String list(EmployeeQueryObject qo, @ModelAttribute("qo")  Employee employee1, HttpSession httpSession, Model model) {
        PageResult<Employee> pageResult = employee.query(qo);
        model.addAttribute("pageResult", pageResult);
        List<Department> departments = department.selectAll();
        model.addAttribute("departments", departments);
        return "employee/list";
    }
        // /employee/delete?id=* , 响应重定向再查询一次
        @RequestMapping("/delete")
        public String delete(Long id) {
            if (id != null) {
                employee.deleteByPrimaryKey(id);
            }
            return "redirect:/employee/list"; // 让浏览器再发送请求到 /employee/list
        }

        // 处理跳转到一个页面请求, 响应 HTML 内容，页面有一个表单

        // 在 webapp 搞一个 jsp, 添加的时候访问那个  jsp（不遵循 MVC 思想）
        // 在 webapp 搞一个 HTML , 添加的时候访问那个 HTML（修改的时候回显数据这个做不到）
        // 编写一个 Servlet 转发到一个 jsp 就可以了
        // 处理方法

        // /employee/input
        @RequestMapping("/input")
        public String input(Long id, Model model) {
            List<Department> departments = department.selectAll();
            model.addAttribute("departments", departments);

            // id 有值  去修改
            // id null 去新增
            if (id != null) { // 去修改
                Employee employee1 = employee.get(id);
                model.addAttribute("employee", employee1);
            }
            return "employee/input";
        }

        // /employee/saveOrUpdate 参数在请求体重
        // 处理保存员工的请求
        @RequestMapping("/saveOrUpdate")
        public String saveOrUpdate(Employee employee1) {
            if (employee1.getId() == null) {
                employee.save(employee1);
            } else {
                employee.update(employee1);
            }
            return "redirect:/employee/list"; // 让浏览器再发送请求到 /employee/list

    }
}
